<?php
/*  goat-cms : tumbleblog content management system
    Copyright (C) 2008 Thomas Purnell tom@thomaspurnell.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

// User modifiable data
define('database_server', 'localhost');   //database server[:port], typically 'localhost'
define('database_username', ""); //username to connect to your mysql server as
define('database_password', ""); //password for mysql access
define('database_id', "goatcms");	 //name of the database you created for goat-cms
define('database_table', "goatcms_test");	 //name of the table you'd like goatcms to create

// Code not requiring user modification

//Open a database connection 
//Connection MUST be closed by calling function using mysql_close()
function db_con(){
	mysql_connect( database_server, database_username, database_password );
	@mysql_select_db(database_id) or die( "Unable to select database");
}

//Delete a database entry given the post ID
function db_delete_entry($deleteID){
	db_con();
	$deleteID = mysql_real_escape_string($deleteID);
	$query = "DELETE from " . database_table . " where ID = " . $deleteID . " LIMIT 1";
	mysql_query( $query ) or die( 'Error deleting entry :( '.$query);
	mysql_close();
	return $result;
}

//Add an entry given the required fields. New post ID is generated by the database
function db_add_entry($subTime, $title, $type, $sourceText, $sourceURL, $caption, $contentA){
	//echo ("db_add_entry: $subTime, $title, $type, $sourceText, $sourceURL, $caption, $contentA");	
	db_con();
	$subTime = mysql_real_escape_string($subTime);	
	$title = mysql_real_escape_string($title);
	$type = mysql_real_escape_string($type);
	$sourceText = mysql_real_escape_string($sourceText);
	$sourceURL = mysql_real_escape_string($sourceURL);
	$caption = mysql_real_escape_string($caption);
	if ($type == "image")
		$contentA = db_generate_image_contentA(mysql_real_escape_string($contentA), $caption, $title);
	else 
		$contentA = mysql_real_escape_string($contentA);	
	$query = "INSERT into " . database_table . " ( ID, SUBTIME, TITLE, TYPE, SOURCETEXT, SOURCEURL, CAPTION, CONTENTA) VALUES ( NULL, '$subTime' , '$title' , '$type' , '$sourceText' , '$sourceURL' , '$caption' , '$contentA')";	
	echo $query;	
	mysql_query( $query ) or die( 'Error adding entry :( '.$query);
	mysql_close();
}

//Generate 'contentA' field for a post of type 'image'. Limits the image width to (currently hardcoded value) 600px. 
function db_generate_image_contentA($filepath, $caption, $title){
	$imgInfo = getimagesize($filepath);
	$width = $imgInfo[0];
	$height = $imgInfo[1];
	$caption;
	$title;
	//Temporary magic number indicating maximum accepted image width without resizing	
	if ($width > 600){
		$scale = 600.0000 / $width;
		$width = 600;
		$height = $height * $scale;
	}
	$retval = "<a href=\"$filepath\" target=\"_blank\"><img class=\"gc_entry_image\" src=\"$filepath\" width=\"$width\" height=\"$height\" alt=\"$alt\" title=\"$title\" /></a>";
	return $retval;
}

//Debugging function, removed soon
function db_debug_listAll(){
	db_con();
	$query = "SELECT * from " . database_table;
	$qresult = mysql_query( $query ) or die( 'Error listing entry :(');
	mysql_close();
	$result = "";
	while ($row = mysql_fetch_array($qresult, MYSQL_ASSOC)){
		$result .= "ID: " . $row['ID'] . " Title: " . $row['title'];
  	}
	return $result;
}

//Return a range of entries given an ID range 
//Not very useful beyond debugging / development purposes
function db_getRange($ID_MIN, $ID_MAX){
	db_con();
	$ID_MIN = mysql_real_escape_string($ID_MIN);
	$ID_MAX = mysql_real_escape_string($ID_MAX);	
	$query = "SELECT * from " . database_table . " where ID >= " . $ID_MIN . " AND ID <= " . $ID_MAX . " ORDER BY ID DESC";
	$result = mysql_query( $query ) or die ( 'Error listing range :( ');
	mysql_close();
	return $result;
}

//Fet a single entry given the ID
function db_getEntry($getID){
	//$getID is sanitised in db_getRange	
	return db_getRange($getID, $getID);
}

//Return count of entries
function db_getCount(){
	db_con();
	$query = "SELECT count(ID) from " . database_table;
	$qresult = mysql_query( $query ) or die ( 'Error getting count :(');
	mysql_close();
	while($row = mysql_fetch_array($qresult)){
		$result = $row['count(ID)'];
	}
	return $result;
}

//Return all entries with a submission time within the given range
function db_getDateRange($DATE_MIN, $DATE_MAX){
	db_con();
	$DATE_MIN = mysql_real_escape_string($DATE_MIN);
	$DATE_MAX = mysql_real_escape_string($DATE_MAX);
	$query = "SELECT * from " .database_table. " where SUBTIME >= '$DATE_MIN' AND SUBTIME <= '$DATE_MAX' ORDER BY ID DESC";
	$result = mysql_query( $query ) or die ( 'Error getting daterange :(');
	mysql_close();
	return $result;
}

?>

